import api from '../../service/index.js'
import {dealurl} from '../../utils/index.js'
Page({
  data: {
    // subjects:[],
    // havetitle:[],
    news: [],
    template:{}
  },
  onLoad (option) {
    let params = option;
    // let title = params.title;
    // wx.setNavigationBarTitle({
    //   title: title
    // });
    let type = params.type;
    delete params.type;
    delete params.title;
    var that = this
    api.templateInfo({
      type:type,
      data:params,
      success:function(res){
        var template = res.data.categoryTemplate
        if (template){
          template.content = JSON.parse(template.content)
        }
        that.setData({
          template: template
        })
       },
      fail: function(err) {
        console.log(err)

      }

    })
      api.news({
        data: { category: option.id, limit: '10', page: 1, sidx:'essence', order: 'desc' },
        success: (res) => {

          var hasMore = this.hasMore
          if (res.data.page.currPage >= res.data.page.totalPage) {
            hasMore = false;
          }
          this.setData({
            news: res.data.page
          })
          wx.stopPullDownRefresh();

        },
        fail: (err) => {

        },
      })
    // api.article({
    //   type:type,
    //   data:params,
    //   success:(res)=>{
    //     let havetitle = res.data.body.subjects.filter((ele)=>{
    //       return ele.title
    //     })
    //     havetitle.forEach((ele,index)=> {
    //       ele.index = index
    //     });
    //     this.setData({
    //       subjects: res.data.body.subjects,
    //       havetitle: havetitle,
    //     });
    //   },
    //   fail:(err)=>{

    //   }
    // })
  },
  toCarousel(event) {
    wx.navigateTo({
      url: '../carousel/carousel?id=' + event.currentTarget.dataset.id,
      success: (res) => { },
      fail: (err) => {
        console.log(err)
      }
    });
  },
  toArticle(event) {
    wx.navigateTo({
      url: '../article/article?id=' + event.currentTarget.dataset.id,
      success: (res) => { },
      fail: (err) => {
        console.log(err)
      }
    });
  },
  toTopic(event) {
    wx.navigateTo({
      url: '../topic/topic?id=' + event.currentTarget.dataset.templateid,
      success: (res) => { },
      fail: (err) => {
        console.log(err)
      }
    });
  },
  toVideo(event) {
    wx.navigateTo({
      url: '../video/video?id=' + event.currentTarget.dataset.id,
      success: (res) => { },
      fail: (err) => {
        console.log(err)
      }
    });
  },
  onPullDownRefresh() {
    this.init()
  },
  onReachBottom() {
    this.loadMore()
  },
  toSwitch(event) {
    let type = event.currentTarget.dataset.type || 0
    switch (type) {
      case 0: this.toArticle(event); break;
      case 1: this.toCarousel(event); break;
      case 2: this.toVideo(event); break;
    }
  },
})
